package com.link.base.base.shorturl.service;

import javax.annotation.Resource;

import com.link.core.cllog.LCLogger;
import com.link.core.kafka.KafkaTemplateFacotry;
import com.link.core.kafka.conf.KafkaEnv;
import com.link.core.util.RedisUtil;
import com.link.core.util.StringUtils;
import com.link.core.util.redisclient.LinkRedisClient;
import com.link.base.base.shorturl.dao.mybatis.mapper.ShortURLMapper;
import org.springframework.stereotype.Service;
import com.link.base.base.shorturl.model.ShortURL;
import com.link.core.basic.dao.mapper.BasicMapper;
import com.link.core.basic.service.BasicServiceImpl;


/**
 * 短链接-
 *
 * @author 姚润发
 * @date 2019/06/04 15:32
 */
@Service
public class ShortURLServiceImpl extends BasicServiceImpl<ShortURL> implements ShortURLService {

    private final String shortURLCacheKeyPrefix = "lnk_short_url#"; // redis缓存key前缀，拼上code作为完整key
    @Resource
    private ShortURLMapper shortURLMapper;

    @Override
    public BasicMapper<ShortURL> getBasicMapper() {
        return shortURLMapper;
    }

    /**
     *
     * @param code 短连接编码，暂时使用id
     * @return
     */
    public String getRedirectURL(String code) {
        LinkRedisClient jedis = null;
        try {
            jedis = RedisUtil.getJedis();

            // 从缓存中获取URL
            String redirectURL = jedis.get(this.shortURLCacheKeyPrefix + code);
            if (StringUtils.isNotBlank(redirectURL)) {
                return redirectURL;
            }

            ShortURL example = new ShortURL();
            // example.setCode(code);
            example.setId(Long.valueOf(code)); // 暂时使用id作为code
            example.setNoCorpId("Y");
            ShortURL result = queryById(example);
            if (result != null) {
                redirectURL = result.getOriginURL();
                jedis.setex(this.shortURLCacheKeyPrefix + code, 60 * 60 * 24 * 7, redirectURL); // 设置url到缓存，7天有效期
                return redirectURL;
            }
        } catch (Exception e) {
            LCLogger.withException("获取短URL出错" + e);
        } finally {
            RedisUtil.returnResource(jedis);
        }
        return null;
    }

    /**
     * 删除数据后删除redis缓存
     * @param record
     */
    @Override
    public void afterDelete(ShortURL record) {
        LinkRedisClient jedis = null;
        try {
            jedis = RedisUtil.getJedis();
            jedis.del(this.shortURLCacheKeyPrefix + record.getId());
        } catch (Exception e) {
            LCLogger.withException("删除短URL缓存出错" + e);
        } finally {
            RedisUtil.returnResource(jedis);
        }
    }

    /**
     * 数据记录更新后更新缓存数据
     * @param record
     * @throws Exception
     */
    @Override
    public void afterUpdate(ShortURL record) throws Exception {
        LinkRedisClient jedis = null;
        try {
            jedis = RedisUtil.getJedis();
            jedis.setex(this.shortURLCacheKeyPrefix + record.getId(), 60 * 60 * 24 * 7, record.getOriginURL()); // 设置url到缓存，7天有效期
        } catch (Exception e) {
            LCLogger.withException("更新短URL缓存出错" + e);
        } finally {
            RedisUtil.returnResource(jedis);
        }
    }

    /**
     * 记录埋点短URL访问的次数
     * @author 李杰
     * @date 2019/08/01 14:37
     * @param code
     */
    @Override
    public void calculateVisits(String code) {
        try {
            //埋点短链接topic：KT_BURY_SHORT_URL
            KafkaTemplateFacotry.send("KT_BURY_SHORT_URL",code);
        } catch (Exception e) {
            KafkaEnv.setEnabled(false);
            LCLogger.withException("短URL访问次数统计出错" + e);
        }
    }
}

